﻿
/*=========================================
* Author: Administrator
* DateTime:2017/6/20 19:22:49
* Description:$safeprojectname$
==========================================*/

using System;
using System.Collections.Generic;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;


namespace OrderSystem
{
    public class CookMediator : Mediator
    {

        private CookProxy cookProxy = null;
        public new const string NAME = "CookMediator";
        public CookView CookView
        {
            get { return (CookView)base.ViewComponent; }
        }

        public CookMediator(CookView view) : base(NAME, view)
        {
            //叫服务员上菜，发送消息通知Command，修改cooker状态，通知服务员上菜
            CookView.ServerFood += item =>
            {
                Debug.Log("服务员来上菜了!!!!!!!!!!!");             
                if (item.state == E_CookState.MakeFood)
                {
                    Debug.LogError("刚工作完的厨师"+item.name +"工作状态为："+ item.state);
                    SendNotification(OrderSystemEvent.MODIFY_COOK_STATE, item);
                }              
            };         
        }

        public override void OnRegister()
        {
            base.OnRegister();
            cookProxy = Facade.RetrieveProxy(CookProxy.NAME) as CookProxy;

            if (null == cookProxy)
                throw new Exception(CookProxy.NAME + "is null");           
            CookView.UpdateCook(cookProxy.Cooks);
        }

        public override IList<string> ListNotificationInterests()
        {
            IList<string> notifications = new List<string>();
            notifications.Add(OrderSystemEvent.UPDATE_COOK_WORK_STATE);
            notifications.Add(OrderSystemEvent.UPDATE_COOKER_REST_STATE);
            return notifications;
        }

        public override void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                case OrderSystemEvent.UPDATE_COOK_WORK_STATE:                  
                    Debug.Log("更新厨师为工作状态");
                    if(notification.Body as IList<CookItem> != null)
                    {
                        CookView.UpdateCook(notification.Body as IList<CookItem>);
                    }                                   
                    break;


                case OrderSystemEvent.UPDATE_COOKER_REST_STATE:
                    if(notification.Body as IList<CookItem> != null)
                    {
                        Debug.LogError("更新厨师为休息状态");
                        CookView.UpdateCook(notification.Body as IList<CookItem>);
                    }                  
                    break;
            }
        }
    }
}